/*
 * Copyright (C) 2015 yixun All Rights Reserved.
 * 
 * ApartmentLayoutDiagramServiceImpl.java
 */
package com.yixun.qfbao.xf.service.impl;

import com.yixun.common.page.Page;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.xf.mapper.ApartmentLayoutDiagramMapper;
import com.yixun.qfbao.xf.model.ApartmentLayoutDiagram;
import com.yixun.qfbao.xf.model.dto.ApartmentLayoutDiagramDto;
import com.yixun.qfbao.xf.model.dto.PremisesHuxingDto;
import com.yixun.qfbao.xf.service.ApartmentLayoutDiagramService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;


/**
 * [文件名称]<br>
 * ApartmentLayoutDiagramServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 户型信息 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2015-10-14 15:34:13 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("apartmentLayoutDiagramService")
public class ApartmentLayoutDiagramServiceImpl implements ApartmentLayoutDiagramService {
	
	@Autowired
	private ApartmentLayoutDiagramMapper mapper;

	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<ApartmentLayoutDiagram> getPageModel(ApartmentLayoutDiagramDto entity,int begin,int end){
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		Integer total = mapper.count(entity);
		List<ApartmentLayoutDiagram> rows = mapper.list(entity,begin,end);
	    Page<ApartmentLayoutDiagram> msr = new Page<ApartmentLayoutDiagram>(total, rows);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<ApartmentLayoutDiagram> selectListByDto(ApartmentLayoutDiagramDto entity){
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ApartmentLayoutDiagram selectByDto(ApartmentLayoutDiagramDto entity){
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public ApartmentLayoutDiagram selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public ApartmentLayoutDiagramDto create(ApartmentLayoutDiagramDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(ApartmentLayoutDiagramDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(ApartmentLayoutDiagramDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(ApartmentLayoutDiagramDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	/**
	 * @param entity 根据pojo搜索如果主键为空则返回它本身
	 * @return 一行记录或本身
	 */
	@Override
	public ApartmentLayoutDiagram selectBy(ApartmentLayoutDiagram entity) {
		if(entity!=null&&entity.getId()!=null){
			entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
			return mapper.selectById(entity.getId());
		}
		return entity;
	}

	/**
	 *  更新户型，1.新增，更新现有的，删除已有的
	 * @param list
	 * @return
	 */
	@Override
	public int editList(List<ApartmentLayoutDiagramDto> list){
		int i = 0;
		for(ApartmentLayoutDiagramDto ad : list){
			if("1".equals(ad.getToDelete())){
				//删除相应户型
				ad.setDelFlag(DelFlagEnum.DEL.getNo());
				i += mapper.edit(ad);
			}else if("0".equals(ad.getToDelete())){
				//编辑相应户型
				ad.setDelFlag(DelFlagEnum.UNDEL.getNo());
				i += saveOrUpdate(ad);
			}
		}
		return i;
	}

	@Override
	public List<PremisesHuxingDto> selectHuxingByPremisesId(Long premisesId) {
		return mapper.selectHuxingByPremisesId(premisesId);
	}

	@Override
	public void delete(ApartmentLayoutDiagramDto entity) {
		List<ApartmentLayoutDiagram> list = selectListByDto(entity);
		if(CollectionUtils.isNotEmpty(list)){
			for (ApartmentLayoutDiagram ad : list) {
				ad.setDelFlag(DelFlagEnum.DEL.getNo());
				mapper.edit(ad);
			}
		}
	}
}
